"use strict";
module.exports = {
    async createCollect(instance, TemplateObject) {
        let OperateStatus;
        let FinalValue;
        let createPromise = new Promise((resolve, reject) => {
            let createCollectPoint = new instance.__InstanceRootControl.PluginPoint.mongodb.MongoClient(
                instance.__libs.Tools.__SpecialTools.getUrl(instance.__InstanceRootControl.confirmFinalData),
                {useUnifiedTopology: true}
            );
            createCollectPoint.connect((error, result) => {
                if (error === null || error === undefined) {
                    result
                        .db(TemplateObject.DataLibrary)
                        .createCollection(TemplateObject.CollectName, (err, res) => {
                            if (err === null || err === undefined) {
                                resolve(`数据集合“${TemplateObject.CollectName}”创建成功。`);
                            } else {
                                reject(`数据集合“${TemplateObject.CollectName}”创建失败，错误信息：${err}`);
                            }
                            createCollectPoint.close(true);
                        });
                } else {
                    reject(error);
                    createCollectPoint.close(true);
                }
            });
        });
        await createPromise.then((val) => {
            OperateStatus = true;
            FinalValue = val
        }).catch((val) => {
            OperateStatus = false;
            FinalValue = val
        });
        if (OperateStatus) {
            return FinalValue;
        } else {
            throw new Error(FinalValue)
        }
    },
    async deleteCollect(instance, TemplateObject) {
        let OperateStatus;
        let FinalValue;
        let deletePromise = new Promise((resolve, reject) => {
            let deleteCollectPoint = new instance.__InstanceRootControl.PluginPoint.mongodb.MongoClient(
                instance.__libs.Tools.__SpecialTools.getUrl(instance.__InstanceRootControl.confirmFinalData),
                {useUnifiedTopology: true}
            );
            deleteCollectPoint.connect((error, result) => {
                if (error === null || error === undefined) {
                    result
                        .db(TemplateObject.DataLibrary)
                        .collection(TemplateObject.CollectName)
                        .drop((err, res) => {
                            if (err === null || err === undefined) {
                                resolve(`数据集合“${TemplateObject.CollectName}”删除成功！`);
                            } else {
                                reject(`数据集合“${TemplateObject.CollectName}”删除失败，错误信息：${err}`);
                            }
                            deleteCollectPoint.close(true);
                        });
                } else {
                    reject(error);
                    deleteCollectPoint.close(true);
                }
            });
        });
        await deletePromise.then((val) => {
            OperateStatus = true;
            FinalValue = val;
        }).catch((val) => {
            OperateStatus = false;
            FinalValue = val;
        });
        if (OperateStatus) {
            return FinalValue;
        } else {
            throw new Error(FinalValue);
        }
    }
}